// 继续的按钮
let nextBtn = document.querySelector(".next");
// 遮罩层
let cover = document.querySelector(".cover");
// 提示框
let sure = document.querySelector(".sure");
// 输入框
let codeInp = document.querySelector("#inpcode");
// 确认键
let okBtn = document.querySelector(".ok");
let exitBtn = document.querySelector(".exitokorno");
let goBtn = document.querySelector(".gookorno");

// 点击继续按钮出现遮罩层、点击提示框以外的区域隐藏遮罩层
nextBtn.addEventListener("click", exitAppears);
exitBtn.addEventListener("click", coverDisOrAppears);
cover.addEventListener("click", coverDisappears);
okBtn.addEventListener("click", logoffUser);
goBtn.addEventListener("click", goBtnClick);

if (!localStorage.getItem("token")) {
  window.location.href = "/";
}

// 发送邮件的提示 出现
function exitAppears() {
  exitBtn.classList.add("active");
}
//发送邮件提示 是否的选项
function coverDisOrAppears(e) {
  if (e.target.className == "exityes") {
    coverAppears();
    exitBtn.classList.remove("active");
  }
  if (e.target.className == "exitno") {
    exitBtn.classList.remove("active");
  }
}

// 遮罩层出现并且发送邮箱
function coverAppears() {
  cover.classList.add("active");
  codeInp.value = "";
  getOffCode();
  codeInp.focus();
}
// 点击true以外的区域。cover消失
function coverDisappears(e) {
  if (e.target == cover) {
    goBtn.classList.add("active");
  }
}
//是否退出注销
function goBtnClick(e) {
  if (e.target.className == "exityes") {
    goBtn.classList.remove("active");
    cover.classList.remove("active");
  }
  if (e.target.className == "exitno") {
    goBtn.classList.remove("active");
  }
}

//向邮箱发送授权码
async function getOffCode() {
  let res = await axios({
    method: "post",
    url: "http://121.37.252.168:8080/api/user/writeOffUserAuthCode",
    headers: { Authorization: `Bearer ${localStorage.token}` }
  });
}
async function logoffUser() {
  let res = await axios({
    method: "post",
    url: "http://121.37.252.168:8080/api/user/writeOffUser",
    headers: {
      Authorization: `Bearer ${localStorage.token}`,
      "Content-Type": "application/json"
    },
    data: {
      authCode: codeInp.value
    }
  });
  console.log(res);
  if (res.data.code == 200) {
    tipsEl.textContent = "账号成功注销，2s后跳转首页";
    tipsEl.classList.add("active");
    //删除token和本地存储的数据
    Dexie.delete("DogTry");
    window.localStorage.clear();
    judgeRemoveTime();

    window.location.href = "../public/indextry.html";
  } else {
    tipsEl.textContent = "授权码错误，请重新输入";
    tipsEl.classList.add("active");
    judgeRemoveTime();
  }
}
